package com.example.taobaounion3.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MenuItem;

import com.example.taobaounion3.R;
import com.example.taobaounion3.base.BaseActivity;
import com.example.taobaounion3.base.baseFragment;
import com.example.taobaounion3.ui.fragment.HomeFragment;
import com.example.taobaounion3.ui.fragment.RedPacketFragment;
import com.example.taobaounion3.ui.fragment.SearchFragment;
import com.example.taobaounion3.ui.fragment.SelectedFragment;
import com.example.taobaounion3.utils.LogUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.zxing.common.StringUtils;
import com.lcodecore.tkrefreshlayout.utils.LogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends BaseActivity {
    private static final String TAG="MainActivity";
    @BindView(R.id.main_navigation_bar)
    public BottomNavigationView mNavigationView;
    private HomeFragment mHomeFragment;
    private SelectedFragment mSelectedFragment;
    private SearchFragment mSearchFragment;
    private RedPacketFragment mRedPacketFragment;
    private baseFragment lastOneFragment=null;
    private FragmentManager mFm;
    private int n=60;
    private Unbinder mBind;



    @Override
    protected void initView() {
        initFrament();
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initEvent() {
        initListener();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }


    private void initFrament() {
        mHomeFragment =new HomeFragment();
        mRedPacketFragment=new RedPacketFragment();
        mSelectedFragment=new SelectedFragment();

        mSearchFragment=new SearchFragment();
        mFm=getSupportFragmentManager();
        switchFragment(mHomeFragment);


    }
    boolean mBoolean=false;




    private void initListener() {
        mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId()==R.id.home){
                    LogUtils.d(this,"选择了首页");
                    switchFragment(mHomeFragment);
                }else if (item.getItemId()==R.id.selected){
                    LogUtils.d(this,"选择了精选");
                    switchFragment(mSelectedFragment);
                }else if (item.getItemId()==R.id.red_packet){
                    LogUtils.d(this,"选择了特惠");
                    switchFragment(mRedPacketFragment);
                }else if (item.getItemId()==R.id.search){
                    LogUtils.d(this,"选择了搜索");
                    switchFragment(mSearchFragment);
                }

                return true;
            }
        });
    }

    private void switchFragment(baseFragment targetFragment) {
        FragmentTransaction fragmentTransaction = mFm.beginTransaction();
        //用replace这种方法进行切换fragment会导致前一个fragment销毁，所以要用add和hide的方法来切换fragment
        if (!targetFragment.isAdded()) {
            fragmentTransaction.add(R.id.main_page_container,targetFragment);
        }else {
            fragmentTransaction.show(targetFragment);
        }
        if (lastOneFragment!=null) {
            fragmentTransaction.hide(lastOneFragment);
        }
        //fragmentTransaction.replace(R.id.main_page_container,baseFragment);
        lastOneFragment=targetFragment;
        fragmentTransaction.commit();
    }

    private void initView1() {

        //mNavigationView=this.findViewById(R.id.main_navigation_bar);

        FragmentManager fm=getSupportFragmentManager();

//        FragmentManager fm1=getSupportFragmentManager();
//        FragmentManager fm2=getSupportFragmentManager();
//        FragmentManager fm3=getSupportFragmentManager();

//
//        FragmentTransaction home_transaction=fm.beginTransaction();
//        home_transaction.add(R.id.main_page_container,homeFragment);
//        home_transaction.commit();

//        RedPacketFragment redPacketFragment=new RedPacketFragment();
//        FragmentTransaction redPacket_transaction=fm1.beginTransaction();
//        redPacket_transaction.add(R.id.main_page_container,homeFragment);
//        redPacket_transaction.commit();
//
//        RelectedFragment relectedFragment=new RelectedFragment();
//        FragmentTransaction relected_transaction=fm2.beginTransaction();
//        relected_transaction.add(R.id.main_page_container,homeFragment);
//        relected_transaction.commit();
//
//        SearchFragment selectedFragment=new SearchFragment();
//        FragmentTransaction search_transaction=fm3.beginTransaction();
//        search_transaction.add(R.id.main_page_container,homeFragment);
//        search_transaction.commit();
    }
}
